﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxGridView;

using Entities;
using BLL;

namespace Stationery.UC
{
    public partial class UC_NewsGroup : System.Web.UI.UserControl
    {
        BLL_NewsGroup bll_NewsGroup = new BLL_NewsGroup();
        Entities.User obj_User = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj_User = (Entities.User)Session[Utils.SESSION_LOGIN];
            if (obj_User == null)
            {
                Response.Redirect(UtilsURL.home);
            }

            if (!IsPostBack)
            {
                BindDataASPxGridView();
            }

            if (Session["datagroup"] == null)
            {
                return;
            }
            else
            {
                DataTable table = (DataTable)Session["datagroup"];
                DataView dv = table.DefaultView;
                ASPxGridViewListNewsGroup.DataSource = dv;
                ASPxGridViewListNewsGroup.DataBind();
            }
            
        }

        #region Bind data into Gridview
        protected void BindDataASPxGridView()
        {
            DataTable dt = new DataTable();
            try
            {                
                if (Session["datagroup"] == null)
                {
                    dt = bll_NewsGroup.Get_All_NewsGroup();
                    if (dt == null)
                        return;
                    else
                        Session["datagroup"] = dt;
                }
                else
                {
                    dt = (DataTable)Session["datagroup"];
                }
                DataView dv = dt.DefaultView;
                ASPxGridViewListNewsGroup.DataSource = dv;
                ASPxGridViewListNewsGroup.DataBind();                    
                
            }
            catch (Exception ex)
            {
                Session["datagroup"] = null;
                dt = null;
            }
        }
        #endregion
        #region Event Click
        protected void ASPxGridViewListNewsGroup_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            //Cancel automatic update
            e.Cancel = true;

            string str_id = e.Keys[ASPxGridViewListNewsGroup.KeyFieldName].ToString();
            try
            {
                bool del = bll_NewsGroup.DeleteNewsGroup(Convert.ToInt32(str_id));
                if (del)
                {
                    Session["datagroup"] = null;
                    BindDataASPxGridView();
                    //LabelError.Text = "Xóa người dùng thành công.";
                }
                else
                {
                    //LabelError.Text = "Xóa người dùng không thành công.";
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        protected void ASPxGridViewListNewsGroup_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            //Cancel automatic update
            e.Cancel = true;
            bool insert = false;
            NewsGroup obj_NewsGroup = new NewsGroup();
            obj_NewsGroup.group_name = Convert.ToString(e.NewValues["Group_name"]).Trim();

            insert = bll_NewsGroup.InsertNewsGroup(obj_NewsGroup);
            if (insert)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Thêm mới thành công!');location.href='{0}'", UtilsURL.newsgroup), true);
                Session["datagroup"] = null;
                BindDataASPxGridView();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Thêm mới KHÔNG thành công!');location.href='{0}'", UtilsURL.newsgroup), true);
                BindDataASPxGridView();
            }
            //back to the browse mode
            ((ASPxGridView)sender).CancelEdit();

        }

        protected void ASPxGridViewListNewsGroup_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            //Cancel automatic update
            e.Cancel = true;

            bool update = false;
            string str_id = "";
            NewsGroup obj_NewsGroup = new NewsGroup();
                        
            str_id = e.Keys[ASPxGridViewListNewsGroup.KeyFieldName].ToString();
            obj_NewsGroup.group_id = Convert.ToInt32(str_id);
            obj_NewsGroup.group_name = Convert.ToString(e.NewValues["Group_name"]).Trim();

            update = bll_NewsGroup.UpdateNewsGroup(obj_NewsGroup);
            if (update)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Cập nhật thành công!');location.href='{0}'", UtilsURL.newsgroup), true);
                Session["datagroup"] = null;
                BindDataASPxGridView();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", String.Format("alert('Cập nhật KHÔNG thành công!');location.href='{0}'", UtilsURL.newsgroup), true);
                BindDataASPxGridView();
            }
            //back to the browse mode
            ((ASPxGridView)sender).CancelEdit();    
        }

        //Event CellEditorInitialize to enable/disable control in grid edit form
        protected void ASPxGridViewListNewsGroup_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
        {
            if (ASPxGridViewListNewsGroup.IsNewRowEditing) return;
            
            if (e.Column.FieldName == "Group_name")
            {
                e.Editor.ClientEnabled = true;
            }                  
        }
        #endregion
    }
}